<template>
  <div>
    <!-- 头部搜索区 -->
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span>搜索名称</span>
      </div>
      <el-form ref="formData" :model="formData" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="作者" placeholder="作者">
              <el-input v-model="formData.author"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标题" placeholder="标题">
              <el-input v-model="formData.title"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标题">
              <el-select v-model="value" placeholder="编辑类型">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row :gutter="20">
        <el-col :span="6" :offset="18">
          <el-button type="primary" size="medium" @click="submit"
            >搜索</el-button
          >
          <el-button type="info" size="medium" @click="reset">重置</el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- table内容区 -->
    <el-card class="table">
      <el-button
        type="primary"
        style="margin-bottom: 10px"
        size="default"
        @click="goEditAricle"
        >新增文章</el-button
      >
      <el-table :data="form" border style="width: 100%">
        <el-table-column type="index" label="#" width="50" align="center">
        </el-table-column>
        <el-table-column
          prop="title"
          align="center"
          label="文章标题"
          width="180"
        >
        </el-table-column>
        <el-table-column label="展示图片" width="180" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.coverImg" alt="" class="img" />
          </template>
        </el-table-column>
        <el-table-column prop="createTime" align="center" label="添加时间">
        </el-table-column>
        <el-table-column prop="author" align="center" label="文章作者">
        </el-table-column>
        <el-table-column label="文章是否显示" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isShow"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="changeShow(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="编辑类型" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.editorType == 0">富文本</div>
            <div v-if="scope.row.editorType == 1">Markdown</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" @click="edit(scope.row)">编辑</el-button>
            <el-button type="warning" @click="delet(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="start"
          :page-sizes="[5, 10]"
          :page-size="limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="full"
        >
        </el-pagination>
      </div>
    </el-card>

    <buttomBanner></buttomBanner>
  </div>
</template>

<script>
import {
  findArticles,
  del,
  changeShowStatus,
} from "@/api/content/article/article.js";
import buttomBanner from "@/components/buttomBanner";
export default {
  data() {
    return {
      form: [],
      formData: {
        author: "",
        title: "",
      },
      full: 0,
      start: 1,
      limit: 5,
      options: [
        {
          value: "选项1",
          label: "富文本",
        },
        {
          value: "选项2",
          label: "MaskDown",
        },
      ],
      value: "",
    };
  },
  components: {
    buttomBanner,
  },
  methods: {
    // 列表展示数据
    init() {
      var selectFrom = {};
      for (let i in this.formData) {
        if (this.formData[i]) {
          selectFrom[i] = this.formData;
        }
      }
      findArticles(this.start, this.limit, this.formData).then((res) => {
        console.log(res);
        if (res.success) {
          this.form = res.data.rows;
          this.full = res.data.total;
        } else {
          this.$message({
            message: res.message,
            type: "error",
          });
        }
      });
    },
    // 切换显示状态
    changeShow(item) {
      changeShowStatus({
        isShow: item.isShow,
        id: item.id,
      }).then((res) => {
        if (res.success) {
          this.$message.success("切换状态成功");
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 新增文章
    goEditAricle() {
      this.$router.push({ name: "ContentEditArticle" });
    },
    // 编辑文章
    edit(mes) {
      this.$router.push({
        path: "/content/editArticle",
        query: { id: mes.id },
      });
    },
    reset() {
      this.formData = {};
      this.start = 1;
      this.limit = 5;
      this.init();
    },
    submit() {
      this.start = 1;
      this.init();
    },
    // 修改展示条数
    handleSizeChange(res) {
      this.limit = res;
      this.start = 1;
      this.init();
    },
    // 下一页
    handleCurrentChange(res) {
      this.start = res;
      this.init();
    },
    delet(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          if (res == "confirm") {
            del(row.id).then((res) => {
              if (res.success) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.init();
              }
            });
          }
        })
        .catch((res) => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.init();
  },
};
</script>

<style scoped lang="scss">
.table {
  margin-top: 30px;
}
.img {
  width: 100px;
  height: 100px;
}
.block {
  margin: 20px auto;
  display: flex;
  justify-content: center;
}
</style>